package com.cwt_taxi_xa.protocol.card;

import com.cwt_taxi_xa.protocol.CardUtil;
import com.cwt_taxi_xa.protocol.card.cmd.AckCallBack;
import com.cwt_taxi_xa.protocol.card.cmd.BaseCmd;
import com.cwt_taxi_xa.util.AppLog;
import com.cwt_taxi_xa.util.TTsFunction;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

/**
 * 无卡片读卡
 * @author Administrator
 *
 */
public abstract class CardBaseState extends CardRFState {

	public String TAG = "CardBaseState";

	List<BaseCmd<AckCallBack>> cmdArray = new ArrayList<>();

	TimerTask timerTask;
	Timer timer;

	public CardBaseState() {

	}

	@Override
	public void startAndRead() {
		super.startAndRead();
		//startTimer();
	}

	@Override
	public void start() {
		AppLog.d(TAG, "start()");
		super.start();
		startTimer();
	}

	@Override
	public void stop() {
		// TODO Auto-generated method stub
		super.stop();
		closeTimer();
		clearCmdArray();
	}

	/*
	 * 结束命令
	 */
	public abstract void endCmd();

	@Override
	public void receivePackage(String mid, String result, byte[] buffer) {
		// TODO Auto-generated method stub
		if(result.equals("9000")){
			if(mid.equals("0020")){
				AppLog.e(TAG,"receivePackage  关闭定时器");
				//关闭定时器
				closeTimer();
				//获取驾驶员扩展信息
				sendFirestCmd();
				//串口测试处理
			}else if(mid.equals("0015")){
				super.receivePackage(mid, result, buffer);
				AppLog.e(TAG,"packageData  0015 打开射频");
			}else if(mid.equals("0016")){
				super.receivePackage(mid, result, buffer);
				AppLog.e(TAG,"packageData  0016 关闭射频");
			}else {
				//分发给接收者
				BaseCmd<AckCallBack> baseCmd = getFirst();
				if(baseCmd != null){
					baseCmd.decode(BaseCmd.byteToSubstring(buffer,8, buffer.length - 5 -6 -2));
				}
			}
		}else if(result.equals("FF0E")){
			AppLog.e(TAG,"不支持的卡座");
		}else if(result.equals("FF0F")){
			AppLog.e(TAG,"未检测到卡片");
			//FF0F和F040都表示非接卡片寻卡失败，一个对应单独发寻卡指令0080[你用不着]，一个对应读从业资格证指令0020
			//start();
		}else if(result.equals("F040")){
			AppLog.e(TAG,"未检测到非接卡片");
		}else if(result.equals("F059")){
			AppLog.e(TAG,"没放TSAM卡");//西安的TSAM卡放在SAM卡槽2
		}else if(result.equals("F040")){
			AppLog.e(TAG,"未检测到非接卡片");//没放卡,查看天线问题
		}else if(result.equals("FF10")){
			AppLog.e(TAG,"卡片认证失败");
		}else if(result.equals("FF15")){
			AppLog.e(TAG,"卡无响应");
		}else if(result.equals("FF16")){
			AppLog.e(TAG,"命令执行失败");
			//start();
		}else if(result.equals("6A88")){
			AppLog.e(TAG,"秘钥不匹配");
		}else if(result.equals("6A83")){
			//命令字第一字节用以区分是标准指令还是厂家自定义指令；第二字节用以定义具体的指令

			//指令出错
			//按目前流程，正常情况下发生在读不到更多车牌记录
			//TTsFunction.speakText("车牌不匹配");
			TTsFunction.speakText("非准驾车辆，无法签到签退");
			AppLog.e(TAG,"非准驾车辆，无法签到签退");
			//moveNextCmd();
		}else{
			if(mid.equals("0020")||mid.equals("0021")){
				AppLog.e(TAG,"读卡失败");
			}
		}
	}

	public long peroid(){
		return 3000;
	};

	protected void clearCmdArray(){
		synchronized (cmdArray) {
			cmdArray.clear();
		}
	}

	protected void sendTsm905Data(int order, byte[] data){
		CardUtil.sendTsm905Data(order, data);
	}

	protected void moveNextCmd() {
		removeFirst();
		BaseCmd<AckCallBack> baseCmd = getFirst();
		if (baseCmd != null) {
			baseCmd.statSend();
		} else {
			AppLog.e(TAG, "moveNextCmd baseCmd is null!");
			endCmd();
		}
	}

	protected void sendFirestCmd(){
		BaseCmd<AckCallBack> baseCmd =	getFirst();
		if(baseCmd != null){
			baseCmd.statSend();
		}else{
			AppLog.e(TAG, "sendFirestCmd cmdArray is null!");
		}
	}

	protected void startTimer(){
		closeTimer();
		AppLog.d(TAG,"startTimer ------------------");
		if(timer == null){
			timer = new Timer();
			timerTask = new  TimerTask() {

				@Override
				public void run() {
					// TODO Auto-generated method stub
					AppLog.d(TAG,"timerTask.run() ------------------");
					CardUtil.sendTsm905Data(0x0020, null);
				}
			};
			//timer.schedule(timerTask, 2000);
			timer.schedule(timerTask, delay(),peroid());
		}
	}

	protected void closeTimer(){
		try {
			if(timerTask != null ){
				timerTask.cancel();
				timerTask = null;
			}
			if(timer != null ){
				timer.cancel();
				timer = null;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}




	protected void addCmd(BaseCmd<AckCallBack> baseCmd){
		synchronized (cmdArray) {
			if(!cmdArray.contains(baseCmd)){
				cmdArray.add(baseCmd);
			}
		}
	}

	@Deprecated
	private void removeCmd(BaseCmd<AckCallBack> baseCmd){
		synchronized (cmdArray) {
			if(cmdArray.contains(baseCmd)){
				cmdArray.remove(baseCmd);
			}
		}
	}

	private void removeFirst(){
		if(cmdArray.size()>0){
			synchronized (cmdArray) {
				cmdArray.remove(0);
			}
		}
	}

	protected BaseCmd<AckCallBack> getFirst(){
		synchronized (cmdArray) {
			if(cmdArray.size()>0){
				return cmdArray.get(0);
			}
		}
		return null;
	}

	@Deprecated
	private BaseCmd<AckCallBack> findBaseCmd(String cmdTag){
		BaseCmd<AckCallBack> baseCmd = null;
		for(int i = 0;i < cmdArray.size();i++){
			if(cmdArray.get(i).getTAG().equals(cmdTag)){
				baseCmd = cmdArray.get(i);
			}
		}
		return baseCmd;
	}

}
